package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class InStockServiceImpl implements InStockService {
    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private SupplierMapper supplierMapper;

    //加载表格数据
    @Override
    public PageVO<InStockVO> findInStockList(Map<String, Object> map) {
        PageHelper.startPage(Integer.valueOf((String)map.get("pageNum")),
                Integer.valueOf((String) map.get("pageSize")));
        Example example = new Example(InStock.class);
        Example.Criteria criteria = example.createCriteria();

        String inNum = (String) map.get("inNum");//单号
        if (inNum != null && !"".equals(inNum)){
            criteria.andEqualTo("inNum",inNum);
        }

        Integer status = Integer.valueOf((String) map.get("status"));
        if (status != null){
            criteria.andEqualTo("status",status);
        }

        if (map.get("type") != null && !"".equals(map.get("type"))){
            Integer type = Integer.valueOf((String) map.get("type"));
            if (type != null){
                criteria.andEqualTo("type",type);
            }
        }

        if (map.get("startTime") != null && map.get("endTime") != null){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String s = (String) map.get("startTime");
            String e = (String) map.get("endTime");
            if (s != null && e != null){
                try {
                    Date startTime = df.parse(s);
                    Date endTime = df.parse(e);
                    criteria.andGreaterThan("createTime",startTime)
                            .andLessThan("createTime",endTime);
                } catch (ParseException ex) {
                    ex.printStackTrace();
                }
            }
        }

        List<InStock> list = inStockMapper.selectByExample(example);
        PageInfo<InStock> pageInfo = new PageInfo<>(list);
        List<InStockVO> listVo = list.stream().map(inStock -> {
            InStockVO inStockVO = new InStockVO();
            BeanUtils.copyProperties(inStock, inStockVO);
            //返回之前要查询其它空缺的属性
            Long supplierId = inStockVO.getSupplierId();
            Supplier s = new Supplier();
            s.setId(supplierId);
            Supplier supplier = supplierMapper.selectByPrimaryKey(s);
            if (supplier != null){
                inStockVO.setPhone(String.valueOf(supplier.getPhone()));
                inStockVO.setSupplierName(supplier.getName());
            }
            return inStockVO;
        }).collect(Collectors.toList());
        PageVO<InStockVO> pageVO = new PageVO<>(pageInfo.getTotal(), listVo);
        return pageVO;
    }

    //移入到回收站
    @Override
    public void remove(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectByPrimaryKey(inStock);
        inStock1.setStatus(1);
        inStockMapper.updateByPrimaryKey(inStock1);
    }

    //还原到已入库
    @Override
    public void back(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectByPrimaryKey(inStock);
        inStock1.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock1);
    }

    @Override
    public void delete(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStockMapper.deleteByPrimaryKey(inStock);
    }

    //添加
    @Override
    public void addIntoStock(InStockVO inStockVO) {
        InStock inStock = new InStock();
        BeanUtils.copyProperties(inStockVO,inStock);
        String uuid = UUID.randomUUID().toString();
        inStock.setInNum(uuid);
        inStock.setOperator("admin");
        Date date = new Date();
        inStock.setCreateTime(date);
        inStock.setModified(date);
        inStock.setStatus(2);
        List<Object> products = inStockVO.getProducts();
        Map map = (Map) products.get(0);
        Integer productNumber = (Integer) map.get("productNumber");
        Integer productId = (Integer) map.get("productId");
        Product product = new Product();
        product.setId(Long.valueOf(String.valueOf(productId)));
        Product product1 = productMapper.selectByPrimaryKey(product);

        ProductStock productStock = new ProductStock();
        productStock.setPNum(product1.getPNum());
        //要先判断是否已经有库存记录了
        Example example = new Example(ProductStock.class);
        example.createCriteria().andEqualTo("pNum",product1.getPNum());
        ProductStock stockFlag = productStockMapper.selectOneByExample(example);
        if (stockFlag == null){
            //库存中没有数据添加
            productStock.setStock(Long.valueOf(String.valueOf(productNumber)));
            productStockMapper.insert(productStock);
        }else {
            //库存中有数据修改
            long stock = stockFlag.getStock() + Long.valueOf(String.valueOf(productNumber));
            stockFlag.setStock(stock);
            productStockMapper.updateByPrimaryKey(stockFlag);
        }
        inStock.setProductNumber(productNumber);

        //把数量添加到库存中
        inStockMapper.insert(inStock);
    }

    @Autowired
    private ProductStockMapper productStockMapper;

    @Autowired
    private ProductMapper productMapper;
    //通过审核
    @Override
    public void publish(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectByPrimaryKey(inStock);
        inStock1.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock1);
    }
}
